package ac.github.os.internal.core.script.impl

import ac.github.os.internal.core.script.BaseWrapper
import ac.github.os.internal.core.script.InternalConfig
import ac.github.os.internal.core.script.InternalScript
import ac.github.os.internal.core.store.Store
import ac.github.os.internal.core.trigger.EntityTrigger
import ac.github.os.internal.core.trigger.Trigger
import ac.github.os.util.getInterfaceT
import ac.github.os.util.getSuperT
import taboolib.common.platform.Awake

@Awake
class GlobalStoreScript : StoreScript() {


    override val name: String
        get() = "store"

}

@Awake
class PlayerStoreScript : StoreScript() {

    override val name: String
        get() = "pstore"

    override val skipEntity: Boolean
        get() = false

}

abstract class StoreScript : InternalScript<StoreScript.Wrapper> {

    class Wrapper : BaseWrapper

    open val skipEntity = true

    override val genericType: Class<*>?
        get() = getInterfaceT(StoreScript::class.java, 0)

    override fun execute(trigger: Trigger?, wrapper: Wrapper, config: InternalConfig, string: String): String {
        val split = string.split(":")
        val type = split[0]
        val key = if (split.size == 1) type else split[1]
        val dataKey =
            (if (trigger != null && !skipEntity && trigger is EntityTrigger) trigger.entity.name + "." else "") + key
        val data = Store.GLOBAL?.get(dataKey) ?: return "null"
        val obj = when (type) {
            "int" -> data.asInteger()
            "long" -> data.asLong()
            "double" -> data.asDouble()
            "boolean" -> data.asBoolean()
            "float" -> data.asFloat()
            else -> data.asString()
        }
        return obj.toString()
    }

}